import { connect } from 'umi';
import { Tabs, Button, Message, Modal } from 'antd';
const { TabPane } = Tabs;
import { exportIcon } from '@/assets';
import { HttpCode } from '#/utils/contacts.js';
import { export_json_to_excel } from '@/utils/fileExport.js';
import { PowerStrategyType } from '@/pages/Tianji/DayRollingTradeStrategy/constant.js';
import { UploadOutlined } from '@ant-design/icons';
import styles from './index.less';
// 导出
import { downloadFileForFront } from '@/tjComponents/TjUpload/TjUploadContent';

import { decisionExport, decisionSave } from '@/pages/Tianji/DayRollingTradeStrategy/services';

const TabList = ({
  menuCode,
  buttonPermissions,
  tradeSeqList,
  tradeSeqId,
  hourOrderList,
  strategyPowerType,
  stations,
  stationId,
  tableDataIsSave,
  tableLimitPower,
  historyQuantityV,
  transactionPowerV,
  transactionPriceType,
  newVersionData,
  dispatch,
  version1,
  version2,
  version3,
}) => {
  const changeCurrentSeqId = (key) => {
    dispatch({
      type: 'dayRollingTradeStragety/setTradeSeqId',
      payload: key,
    });
  };

  // 导出策略
  const exportStrategy = async () => {
    if (
      !buttonPermissions.filter((item) => item.menuCode === 'RollingBatchStrategyExport').length
    ) {
      return Message.info('请联系管理员获取相关权限');
    }
    const filename =
      tradeSeqList.filter((item) => item.tradeSequenceId === tradeSeqId)[0].simpleName +
      '_' +
      stations.filter((item) => item.value === stationId)[0].label +
      '日滚策略';
    const versionData = {
      ...newVersionData,
      limitPowerType: tableLimitPower,
      historyQuantityV: historyQuantityV,
      transactionPowerV: transactionPowerV,
      transactionPriceType: transactionPriceType,
      ...(version1 && { forecastPowerV: version1 }),
      ...(version2 && { forecastPriceV: version2 }),
      forecastPriceType: version3,
    };
    const params = {
      versionData,
      modelDataList: hourOrderList,
    };
    const res = await decisionExport(params);
    downloadFileForFront(res, `${filename}.xlsx`);
  };

  // 保存策略
  const saveAsync = async () => {
    if (!buttonPermissions.filter((item) => item.menuCode === 'RollingBatchStrategySave').length) {
      return Message.info('请联系管理员获取相关权限');
    }
    if (tableDataIsSave === '1') {
      Modal.confirm({
        title: '确认保存',
        content: '该数据已经保存，是否重新保存？',
        okText: '确定',
        cancelText: '取消',
        onOk() {
          saveData();
        },
        onCancel() {
          return;
        },
      });
    } else {
      saveData();
    }
  };

  const saveData = async () => {
    const versionData = {
      ...newVersionData,
      limitPowerType: tableLimitPower,
      historyQuantityV: historyQuantityV,
      transactionPowerV: transactionPowerV,
      transactionPriceType: transactionPriceType,
      ...(version1 && { forecastPowerV: version1 }),
      ...(version2 && { forecastPriceV: version2 }),
      forecastPriceType: version3,
    };
    dispatch({
      type: 'dayRollingTradeStragety/setAddLoadingNum',
    });
    const params = {
      versionData,
      modelDataList: hourOrderList,
    };
    const res = await decisionSave(params);
    dispatch({
      type: 'dayRollingTradeStragety/setSubLoadingNum',
    });
    if (res?.statusCode === HttpCode.SUCCESS) {
      Message.success('保存策略成功');
    } else {
      Message.error(res.message || '保存策略失败');
    }
  };

  return (
    <div className={styles.tabList}>
      <div className={styles.tabs}>
        <Tabs activeKey={tradeSeqId + ''} onChange={(id) => changeCurrentSeqId(id)}>
          {tradeSeqList.map((item) => {
            return <TabPane tab={item.simpleName} key={item.tradeSequenceId} />;
          })}
        </Tabs>
      </div>
      <div className={styles.operate}>
        <Button className={styles.btn} onClick={saveAsync}>
          保存
        </Button>
        <div className={styles.exportBtn} onClick={exportStrategy}>
          {/* <img className={styles.icon} src={exportIcon} /> */}
          <UploadOutlined className={styles.icon} />
          <span>导出</span>
        </div>
      </div>
    </div>
  );
};

export default connect(({ dayRollingTradeStragety, global }) => ({
  menuCode: global.configure.menuCode,
  buttonPermissions: global.configure.buttonPermissions,
  tradeSeqList: dayRollingTradeStragety.tradeSeqList,
  tradeSeqId: dayRollingTradeStragety.tradeSeqId,
  hourOrderList: dayRollingTradeStragety.hourOrderList,
  strategyPowerType: dayRollingTradeStragety.strategyPowerType,
  tableDataIsSave: dayRollingTradeStragety.tableDataIsSave,
  stations: dayRollingTradeStragety.stations,
  stationId: dayRollingTradeStragety.stationId,
  tableLimitPower: dayRollingTradeStragety.tableLimitPower,
  historyQuantityV: dayRollingTradeStragety.historyQuantityV,
  transactionPowerV: dayRollingTradeStragety.transactionPowerV,
  transactionPriceType: dayRollingTradeStragety.transactionPriceType,
  newVersionData: dayRollingTradeStragety.newVersionData,
  version1: dayRollingTradeStragety.version1,
  version2: dayRollingTradeStragety.version2,
  version3: dayRollingTradeStragety.version3,
}))(TabList);
